﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApp.Services;
using ConsoleApp.Dapper;
using log4net;

namespace WebApp.Controllers
{
    public class HomeController : Controller
    {
        protected static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        private ICategoryService _categoryService = null;

        public HomeController(ICategoryService categoryService)
        {
            _categoryService = categoryService;
        }

        public ActionResult Index(int? id)
        {
            try
            {
                IList<Day> dyas = _categoryService.GetCategories(1, 10);

                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                if (dyas.Count == 0)
                {
                    sb.AppendLine("no day found in db so I decided to insert one day by myself,and you will see this content\r\n");

                    Day day = new Day { DayName = "day insert" };
                    _categoryService.Insert(day);
                    dyas.Add(day);
                }

                foreach (Day day in dyas)
                {
                    sb.AppendLine(day.DayName + " --- " + day.AutoId);
                }

                log.Info(sb.ToString() + "----" + Request.ServerVariables["REMOTE_ADDR"] + "----" + Request.UserHostAddress);
                return Content(sb.ToString() + "----" + Request.ServerVariables["REMOTE_ADDR"] + "----" + Request.UserHostAddress);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                return Content(ex.Message);
            }
        }
    }
}
